package com.sz.app.oss.api.logistics.base;


import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.approval.Approval;
import com.sz.biz.common.approval.ApprovalService;
import com.sz.biz.common.approval.ApprovalStatus;
import com.sz.biz.logistics.bill.entity.BillingStatus;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.core.system.service.SysUserSupport;
import com.sz.common.core.system.service.UserModel;
import com.sz.app.oss.api.logistics.base.dto.ApprovalDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Function: 审核控制 controller <br>
 * Author: hpeng.wang <br>
 * Date: 2016-12-07 11:08:00
 */

@RestController
@RequestMapping("${api}/biz/approval")
@Api(description = " ", tags = "022、审核流程")
public class ApprovalController extends BaseController {
    @Autowired
    private ApprovalService approvalService;

    @Autowired
    SysUserSupport userSupport;

    /**
     * 根据id审批流程
     *
     * @param uid 数据id
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/service/{uid}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据服务uid获取审批流程")
    public ResultDto findServiceApprovals(@ApiParam(value = "服务uid", required = true) @PathVariable String uid) {
        ResultDto result = new ResultDto();
        List<ApprovalDto> dtos = getApprovals(ApprovalService.ENTITY_TYPE_SERVICE, uid);
        result.setData(dtos);
        return result;
    }

    /**
     * 根据id审批流程
     *
     * @param uid 数据id
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/price/{uid}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据价格uid获取审批流程")
    public ResultDto findPriceApprovals(@ApiParam(value = "价格uid", required = true) @PathVariable String uid) {
        ResultDto result = new ResultDto();
        List<ApprovalDto> dtos = getApprovals(ApprovalService.ENTITY_TYPE_PRICE, uid);
        result.setData(dtos);
        return result;
    }

    /**
     * 根据id审批流程
     *
     * @param uid 数据id
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/product/{uid}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据产品uid获取审批流程")
    public ResultDto findProductApprovals(@ApiParam(value = "产品uid", required = true) @PathVariable String uid) {
        ResultDto result = new ResultDto();
        List<ApprovalDto> dtos = getApprovals(ApprovalService.ENTITY_TYPE_PRODUCT, uid);
        result.setData(dtos);
        return result;
    }
    /**
     * 根据billNo审批流程
     *
     * @param billNo
     * @return 查询到的审批流程
     */
    @RequestMapping(value = "/financial/{billNo}", method = RequestMethod.GET)
    @ApiOperation(value = "查询审批流程", notes = "根据账单号获取审批流程")
    public ResultDto findFinancialApprovals(@ApiParam(value = "billNo", required = true) @PathVariable String billNo) {
        ResultDto result = new ResultDto();
        List<ApprovalDto> dtos = getApprovals(ApprovalService.ENTITY_TYPE_FINANCIAL, billNo);
        result.setData(dtos);
        return result;
    }

    private List<ApprovalDto> getApprovals(String entityType, String entityId) {
        List<ApprovalDto> dtos = new ArrayList<>();
        List<Approval> list = approvalService.getApprovals(entityType, entityId);
        if (list != null && list.size() > 0) {
            List<Integer> userIds = new ArrayList<>();
            list.forEach(approval -> userIds.add(approval.getApprovedBy()));
            Map<Integer, UserModel> map = userSupport.getUsersMapById(userIds);
            for (Approval approval : list) {
                ApprovalDto dto = new ApprovalDto(approval.getApprovalOpinion(), approval.getApprovalTime());
                if (entityType.equals(ApprovalService.ENTITY_TYPE_FINANCIAL)) {
                    dto.setAction(approvalService.getActionName(BillingStatus.DRAFT,approval));
                } else {
                    dto.setAction(approvalService.getActionName(ApprovalStatus.Draft,approval));
                }
                int userId = approval.getApprovedBy();
                if (map.containsKey(userId)) {
                    dto.setOperator(map.get(userId).getFullName());
                }
                dtos.add(dto);
            }
        }
        return dtos;
    }

}



